package Scuttlebutt;

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.tree.*;
import java.util.*;
import glue.ftp.*;
import java.lang.Runnable;

public class FolderPopupMenu extends JPopupMenu implements FtpObserver
{
	
	long num_of_bytes = 0;  
	
/*	public class DeleteDirContents extends JMenuItem implements ActionListener{
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public DeleteDirContents(Scuttlebutt parent, int x, int y){
			super("Delete Folder Contents");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0){
			FolderNode o = (FolderNode)parent.getProject().getTree().getLastSelectedPathComponent();
			String path = new String(new String((o.getRemoteDirectory().length() > 0)?new String(o.getRemoteDirectory() + File.separatorChar):"") + o);
		}
	}
*/	
	
	
	public class DeleteDir extends JMenuItem implements ActionListener{
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public DeleteDir(Scuttlebutt parent, int x, int y){
			super("Delete Folder");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0){
			FolderNode o = (FolderNode)parent.getProject().getTree().getLastSelectedPathComponent();
			String path = new String(new String((o.getRemoteDirectory().length() > 0)?new String(o.getRemoteDirectory() + File.separatorChar):"") + o);

			String msg = new String("Are you sure you want to delete \'" + path + "\' and all of its contents? This cannot be undone.");
			if(JOptionPane.showConfirmDialog(parent, msg, "Warning",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
				try{
					parent.getFTP().removeDirectory(path);
				}catch(java.io.IOException e){
					System.err.println(e);
				}catch(FtpException f){
					System.err.println("Failed to delete folder:" + f);
				}
				
				parent.getEditPane().remove(o.getNodePath());
				
				File f = new File(o.getNodePath());
				f.delete();
				
				
				o.removeFromParent();
				
				parent.updateTree();
			}else{
			}
		}
	}
	
	
	public class CreateDir extends JMenuItem implements ActionListener{
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public CreateDir(Scuttlebutt parent, int x, int y){
			super("New Folder...");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0){
			System.out.println("CreateDir");

			FolderNode o = (FolderNode)parent.getProject().getTree().getLastSelectedPathComponent();
			StringInputDialog dlg = new StringInputDialog(type.folder, parent, new String("New Folder"), new String("Name"), new String("New Folder"), o.getNodePath());
			dlg.setVisible(true);
			if(dlg.getState()){
				o.addDirectory(parent, dlg.getValue());
			}
		}
	}	
	
	public class CreateHTMLFile extends JMenuItem implements ActionListener{	
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		public CreateHTMLFile(Scuttlebutt parent, int x, int y){
			super("Create HTML File...");
			addActionListener(this);
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		
		public void actionPerformed(ActionEvent a0){
			FolderNode o = (FolderNode)parent.getProject().getTree().getLastSelectedPathComponent();
			if(o.getNodePath() == null){
				System.out.println("dir is null");
				return;
			}
		
			if(parent.getProject() != null){
				StringInputDialog dlg = new StringInputDialog(type.file, parent, new String("New Filename"), new String("Name"), new String("temp.html"), o.getNodePath());
				dlg.setVisible(true);
				if(dlg.getState()){
					try{
						o.addFile(parent, dlg.getValue());
					}catch (IOException e){
						System.err.println("failed to create file:" + e.getMessage());
					}
				}
			}
		}	
	}	

	public class Upload extends JMenuItem implements ActionListener{	
		Scuttlebutt parent = null;
		int x = 0;
		int y = 0;
		FolderPopupMenu menu = null;
		Thread thisThread = null;
		public Upload(Scuttlebutt parent, int x, int y, FolderPopupMenu menu){
			super("Upload");
			addActionListener(this);
			this.menu = menu;
			this.x = x;
			this.y = x;
			this.parent = parent;
		}
		public void actionPerformed(ActionEvent a0){
			new Thread(new AddThread(parent, menu)).start();
		}
	}	
	
	class AddThread implements Runnable{	
		Scuttlebutt parent = null;
		FolderPopupMenu menu = null;
		public AddThread(Scuttlebutt parent, FolderPopupMenu menu){
			this.parent = parent;
			this.menu = menu;
		}
		
		public void run(){
			FolderNode o = (FolderNode)parent.getProject().getTree().getLastSelectedPathComponent();
//			if(o.getRemoteDirectory().compareToIgnoreCase(parent.getRemoteHomeDirectory()) != 0)
				uploadDirectory(parent, o);
				
			recurseDirectory(parent, o, menu);
			parent.getFileTransferTable().startDownloading();
		}
	}
	
	
	public FolderPopupMenu(Scuttlebutt parent, int x, int y)
	{
		add(new CreateDir(parent, x, y));
		add(new DeleteDir(parent, x, y));
//		add(new DeleteDirContents(parent, x, y));
		add(new EventNewFile(new String("New File..."), parent));
		add(new Upload(parent, x, y, this));
		
		FolderNode o = (FolderNode)parent.getProject().getTree().getLastSelectedPathComponent();
		if(parent.getProject().getProjectTitle().compareToIgnoreCase(o.toString()) == 0){
			((JMenuItem)getComponent(1)).setEnabled(false);
		}
		
		
		if(parent.isConnected()){
			((JMenuItem)getComponent(3)).setEnabled(true);
		}else{
			((JMenuItem)getComponent(3)).setEnabled(false);
		}
	}
	
	
	
	private void recurseDirectory(Scuttlebutt parent, FolderNode o, FolderPopupMenu menu){
		for (Enumeration e = o.children() ; e.hasMoreElements() ;) {
			Object node = (Object)e.nextElement();
	        if (node instanceof FolderNode ) {
		        FolderNode f = (FolderNode)node;
		        if(!f.isUploaded()){
		        	uploadDirectory(parent, f);
		        	f.setUploaded(true);
		        }
		        recurseDirectory(parent, f, menu);
	        }else if(node instanceof URLNode){
	        }else if (node instanceof SortableNode ){
		        SortableNode s = (SortableNode)node;
		        if(!s.getUploadingState()){
					Tab tab = parent.getEditPane().search(s.getNodePath());
					if(tab != null){
						tab.Save();
						tab.setChanged();
					}
					Download d = new Download(parent, s);
					parent.getFileTransferTable().addDownload(d);
		        }
	        }
		}
		
	}
	
	private void uploadDirectory(Scuttlebutt parent, FolderNode o){
		try{
			parent.getFTP().setDirectory(parent.getRemoteHomeDirectory());
			
			if(o.getRemoteDirectory().length() != 0){
				parent.getFTP().setDirectory(new String(o.getRemoteDirectory()));
			}
			
//			System.out.println("upload directory:" + o.toString());
			if(!o.isUploaded()){
//				System.out.println("!uploaded");
				parent.getFTP().makeDirectory(o.toString());
				o.setUploaded(true);
				parent.updateTree();
			}else{
//				System.out.println("uploaded");
			}
			
		}catch(IOException e){
			System.err.println("uploadDirectory IOE: " + e.getMessage());
		}catch(FtpException i){
			System.err.println("uploadDirectory ftp.FtpException: " + i.getMessage());
		}
	}
	
	public void byteRead(int i0)
	{
		num_of_bytes += i0;
	}

	public void byteWrite(int i0)
	{
	}

}

